package com.tarena.lbs.xxl.job.job.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.marketing.api.MarketingApi;
import com.tarena.lbs.pojo.stock.dto.CouponStockDTO;
import com.tarena.lbs.stock.api.StockApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TimingCopyStockService {
    @Autowired
    private MarketingApi marketingApi;
    @Autowired
    private StockApi stockApi;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void copyStock() {
        List<Integer> availableIds = marketingApi.getAvailableIds();
        List<CouponStockDTO> couponNum = null;
        if (CollectionUtils.isNotEmpty(availableIds)){
            couponNum = stockApi.getCouponNum(availableIds);
        }
        if (CollectionUtils.isNotEmpty(couponNum)){
            for (CouponStockDTO couponStockDTO : couponNum) {
                //循环一次 构造一个key 写入redis
                String couponStockKey ="coupon:stock:"+couponStockDTO.getCouponId();
                String value = couponStockDTO.getNum()+"";
                stringRedisTemplate.opsForValue().set(couponStockKey,value);
            }
        }
    }
}
